package service.provider;

import dao.provider.ProviderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Provider;
import pojo.Role;

import java.util.List;

@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private ProviderMapper providerMapper;

    public ProviderMapper getProviderMapper() {
        return providerMapper;
    }

    public void setProviderMapper(ProviderMapper providerMapper) {
        this.providerMapper = providerMapper;
    }

    public Provider getProviderByProCode(String proCode) {
        try {
            return providerMapper.getProviderByProCode(proCode);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public int getProviderCount(String queryProName, String queryProAddress) {
        // TODO Auto-generated method stub
        int count = 0;
        try {
            count=providerMapper.getProviderCount(queryProName,queryProAddress);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;
    }

    public List<Provider> getProviderListByPage(String proName, String proAddress, Integer currentPageNo, Integer pageSize) {
        // TODO Auto-generated method stub
        List<Provider> providerList = null;

        try {
            providerList=providerMapper.getProviderListByPage(proName,proAddress,currentPageNo,pageSize);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return providerList;
    }

    public List<Provider> getProviderList() {
        List<Provider> providerList = null;
        try {
            providerList=providerMapper.getProviderList();
        }
        catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return providerList;
    }

    public boolean addProvider(Provider provider) {
        boolean result=false;
        int count= 0;
        try {
            count = providerMapper.addProvider(provider);
            if (count>0){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Provider getProviderById(Integer pid) {
        Provider provider=null;
        try {
            provider = providerMapper.getProviderById(pid);
        } catch (Exception e) {
            e.printStackTrace();
            provider=null;
        }
        return provider;
    }

    public boolean modify(Provider provider) {
        boolean result=false;
        try {
            int count=providerMapper.modifyProvider(provider);
            if (count>0){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Provider selectProCodeExist(String proCode) {
        Provider provider=null;
        try {
            provider=providerMapper.getProviderByProCode(proCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return provider;
    }

    public boolean deleteProviderById(int id) {
        boolean result = false;
        try {
            if(providerMapper.deleteProviderById(id) > 0)
                result = true; //删除成功
            else
                result=false;  //删除失败
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}

